package com.yuwenxin.dao.impl;

import com.yuwenxin.bean.Album;
import com.yuwenxin.core.BaseDaoImpl;
import com.yuwenxin.core.BaseQueryModel;
import com.yuwenxin.dao.AlbumDao;

import java.util.Date;
import java.sql.ResultSet;
import java.util.HashSet;

public class AlbumDaoImpl extends BaseDaoImpl implements AlbumDao {
    @Override
    public String getSelectAllSql() {
        return "select * from album";
    }

    @Override
    public String getSelectByNameSql() {
        return "select * from album where albumName=?";
    }

    @Override
    public String getFuzzyFindSql() {
        return "select from album where albumName like ?";
    }

    @Override
    public String getUpdateSql(Object object) {
        return null;
    }

    @Override
    public String getDeleteSql() {
        return "delete from album where idalbum=?";
    }

    @Override
    public String getSelectByIdSql() {
        return "select * from album where idalbum = ?";
    }

    @Override
    public String getInsertSql(Object object) {
        String albumName = ((Album) object).getAlbumName();
        String corporationName = ((Album) object).getCorporationName();
        String albumPhotoPath = AlbumDao.DEFAULT_PATH;  //默认目录被保存在接口中
        Date publishDate = ((Album) object).getPublishDate();
        // 默认认为只有corporationName可能为空，专辑名和发布日期为必填参数

        if (corporationName == null){
            StringBuilder sb = new StringBuilder("insert into album (albumName,albumPhotoPath,publishDate) values('");
            System.out.println("未输入公司名称");
            sb.append(albumName).append("','").append(albumPhotoPath).append("','").append(publishDate).append("')");
            return sb.toString();
        }
        StringBuilder sb = new StringBuilder("insert into album (albumName,corporationName,albumPhotoPath,publishDate) values('");
        sb.append(albumName).append("','").append(corporationName).append("','").append(albumPhotoPath).append("','")
                .append(publishDate).append("')");
        return sb.toString();
    }

    @Override
    public String getConditionSql(BaseQueryModel queryModel) {
        return null;
    }

    @Override
    public Object toEntity(ResultSet rs) throws Exception {
        Album album = new Album();
        album.setId(rs.getInt("idalbum"));
        album.setAlbumName(rs.getString("albumName"));
        album.setCorporationName(rs.getString("corporationName"));
        album.setAlbumPhotoPath(rs.getString("albumPhotoPath"));
        album.setPublishDate(rs.getDate("publishDate"));
        album.setMusicSet(new HashSet<>());  // 这里要通过MusicDao来获取一个"select * from music where album_id = ?"的外键查询语句
        return album;
    }
}
